package com.jiang.www;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;

@WebServlet("/paramServlet")
public class ParamServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //防止乱码
        req.setCharacterEncoding("utf-8");
        System.out.println("doPost");
        //获取参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sex = req.getParameter("sex");
        //多个相同key 获取数组
        String[] hobby = req.getParameterValues("hobby");
        System.out.println(username + password + sex + Arrays.toString(hobby));
        System.out.println("=========================");
        //获取所有参数  map
        Map<String, String[]> map = req.getParameterMap();
        //获取map值
        for (String s : map.keySet()) {
            String[] values = map.get(s);
            System.out.println(Arrays.toString(values));
        }
//        Iterator<String> it = map.keySet().iterator();
//        while (it.hasNext()) {
//            String key = it.next();
//            String[] value = map.get(key);
//        }
    }
}
